package com.wyl.mobileplayer.ui.activity;

import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.wyl.mobileplayer.R;
import com.wyl.mobileplayer.ui.base.BaseFragment;
import com.wyl.mobileplayer.ui.fragment.AudioFragment;
import com.wyl.mobileplayer.ui.fragment.NetAudioFragment;
import com.wyl.mobileplayer.ui.fragment.NetVideoFragment;
import com.wyl.mobileplayer.ui.fragment.VideoFragment;
import com.wyl.mobileplayer.utils.PermissionUtils;


/**
 * 时间：2018/6/24 0024 13:38
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 * 内容:
 * 1.RadioGroup + FrameLayout 防止重影的设置
 *
 * @author WangYoule
 * @qq 270628297
 */
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radio_group;
    private Toolbar toolbar;

    private BaseFragment[] fragments;
    private int currentIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, new PermissionUtils.PermissionGrant() {
            @Override
            public void onPermissionGranted(int requestCode) {

            }
        });

        initPager();
        initView();
        initData();
        initListener();

    }

    private void initData() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    private void initListener() {
        radio_group.setOnCheckedChangeListener(this);
        radio_group.check(R.id.radioButton0);
    }

    private void initPager() {
        fragments = new BaseFragment[]{
                new VideoFragment(), new AudioFragment(),
                new NetVideoFragment(), new NetAudioFragment()
        };

        FragmentManager manager = getSupportFragmentManager();
        for (int i = 0; i < fragments.length; i++) {
            Fragment fragment = manager.findFragmentByTag(String.valueOf(i));
            if (fragment != null) {
                manager.beginTransaction().remove(fragment).commit();
            }
        }

    }

    private void initView() {
        radio_group = (RadioGroup) findViewById(R.id.radio_group);
        toolbar = findViewById(R.id.toolbar);
    }

    // 底部导航的选择监听
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (R.id.radioButton0 == checkedId) {
            showFragment(0);
        } else if (R.id.radioButton1 == checkedId) {
            showFragment(1);
        } else if (R.id.radioButton2 == checkedId) {
            showFragment(2);
        } else if (R.id.radioButton3 == checkedId) {
            showFragment(3);
        }
    }

    private void showFragment(int position) {
        if (currentIndex == position) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragments[position].isAdded()) {
            transaction.show(fragments[position]);
        } else {
            transaction.add(R.id.fl_container, fragments[position], String.valueOf(position));
        }
        if (currentIndex != -1) {
            transaction.hide(fragments[currentIndex]);
        }
        transaction.commit();
        currentIndex = position;

//        getSupportFragmentManager().beginTransaction()
//                .replace(R.id.fl_container, fragment)
//                .commit();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, new PermissionUtils.PermissionGrant() {
            @Override
            public void onPermissionGranted(int requestCode) {

            }
        });
    }
}
